package com.mobile.collect.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.ListPopupWindow;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.mobile.collect.R;
import com.mobile.collect.interfaces.PopWindowInterface;

import java.util.ArrayList;

/**
 * @author: Kongzue
 * @github: https://github.com/kongzue/
 * @homepage: http://kongzue.com/
 * @mail: myzcxhh@live.cn
 * @createTime: 2019/9/29 12:14
 */
public class PopWindowEditText extends AppCompatEditText implements PopWindowInterface {

    public PopWindowEditText(Context context) {
        super(context);
        init(context, null);
    }

    public PopWindowEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PopWindowEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private String[] menuList;

    private void init(Context context, AttributeSet attrs) {
        String menuListCache = null;
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PopWindowEditText);
            menuListCache = typedArray.getString(R.styleable.PopWindowEditText_menu);
            typedArray.recycle();
        }
        if (menuListCache != null && menuListCache.length() > 0) {
            if (menuListCache.contains(",")) {
                menuList = menuListCache.split(",");
            }
        }
        if (!isInEditMode()) {
            setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    //showListPopulWindow();
                }
            });

            addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    String inputStr = s.toString();
                    if (inputStr.length() != 0) {
                        if (needMenu && !isNotInputEvent) {
                            showListPopWindow(inputStr);
                        }
                        isNotInputEvent = false;
                    } else {
                        needMenu = true;
                        if (listPopupWindow != null) listPopupWindow.dismiss();
                    }
                }
            });
        }
    }

    private boolean isNotInputEvent = false;

    @Override
    public void setText(CharSequence text, BufferType type) {
        isNotInputEvent = true;
        super.setText(text, type);
    }

    public void showMenu() {
        needMenu = true;
        showListPopWindow("");
    }

    private ListPopupWindow listPopupWindow;
    private boolean needMenu = true;
    ArrayList<String> menu;
    ArrayList<String> spitMenu;

    private void showListPopWindow(String inputString) {
        Log.i(">>>", "isEnabled: " + isEnabled());
        if (!isEnabled()) {
            return;
        }
        if (menuList != null && menuList.length != 0) {
            menu = new ArrayList<>();
            for (String s : menuList) {
                if (inputString.length() == 0) {
                    menu.add(s);
                } else {
                    if (s.toLowerCase().contains(inputString.toLowerCase())) {
                        menu.add(s);
                    }
                }
            }
            if (listPopupWindow != null) {
                listPopupWindow.dismiss();
            }
            if (menu.size() == 0) {
                return;
            }

            spitMenu = new ArrayList<>();
            for (String value : menu) {
                if (value.contains("@")) {
                    String[] valueArray = value.split("@");
                    spitMenu.add(valueArray[1]);
                }else{
                    spitMenu.add(value);
                }
            }

            listPopupWindow = new ListPopupWindow(getContext());
            listPopupWindow.setAdapter(
                    new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, spitMenu)
            );
            listPopupWindow.setAnchorView(this);//以哪个控件为基准，在该处以mEditText为基准
            listPopupWindow.setModal(false);

            listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置项点击监听
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    needMenu = false;
                    setText(spitMenu.get(i));//把选择的选项内容展示在EditText上
                    setSelection(getText().toString().length());
                    listPopupWindow.dismiss();//如果已经选择了，隐藏起来
                }
            });
            listPopupWindow.show();//把ListPopWindow展示出来
        }
    }

    public PopWindowEditText setMenuList(String[] menuList) {
        this.menuList = menuList;
        return this;
    }

    public PopWindowEditText setMenuList(String menuListCache) {
        if (menuListCache != null && menuListCache.length() > 0) {
            if (menuListCache.contains(",")) {
                menuList = menuListCache.split(",");
            }
        }
        return this;
    }

    public void setMenuList(ArrayList<String> descriptionList) {
        this.menuList = descriptionList.toArray(new String[descriptionList.size()]);
    }
}
